<template>
  <div>
    <!--Breadcrumb面包屑组件-->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <el-row>
        <el-col>
          <el-button type="primary" @click="getAddCateDialog"
            >添加商品分类</el-button
          >
        </el-col>
      </el-row>

      <tree-table
        class="treeTable"
        :selection-type="false"
        :data="cateList"
        :columns="cateColumnsCfg"
        show-header
        border
        stripe
        show-index
        index-text="#"
        :expand-type="false"
      >
        <template slot="cat_deleted" scope="scope">
          <i
            :class="[
              !scope.row.cat_deleted ? 'el-icon-success' : 'el-icon-error'
            ]"
          ></i>
        </template>
        <template slot="cat_level" scope="scope">
          <el-tag size="mini" :type="cateLevelTag[scope.row.cat_level].type">{{
            cateLevelTag[scope.row.cat_level].content
          }}</el-tag>
        </template>
        <template slot="setting" scope="scope">
          <el-button
            type="primary"
            icon="el-icon-edit"
            size="mini"
            @click="setCate(scope)"
            >编 辑</el-button
          >
          <el-button type="danger" icon="el-icon-delete" size="mini"
            >删 除</el-button
          >
        </template>
      </tree-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="cateInfo.pagenum"
        :page-sizes="[10, 15, 20, 25]"
        :page-size="cateInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!--添加分类对话框-->
    <el-dialog
      title="添加分类"
      :visible.sync="addCateDialog"
      class="addCateDialog"
      width="50%"
      @close="addCateDialogClose"
    >
      <el-form
        :model="addCateForm"
        label-width="80px"
        :rules="addCateRules"
        ref="addCateFormRef"
      >
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addCateForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类">
          <!--cascader级联选择器组件-->
          <!--:options指定数据源，:prop配置数据渲染方案-->
          <!--这里v-model必须绑定数组-->
          <!--change-on-select是否可以单独选择父级-->
          <!--clearable change-on-select :options="parentCateList" -->
          <el-cascader
            style="width: 100%"
            clearable
            change-on-select
            v-model="selectedKeys"
            :options="parentCateList"
            :props="addCateCascaderProp"
            @change="parentCateChange"
          ></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button type="primary" @click="addCate">添 加</el-button>
        <el-button @click="addCateDialog = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "categories",
  data() {
    return {
      cateList: [],
      parentCateList: [],
      //  级联选择器prop配置对象
      addCateCascaderProp: {
        expandTrigger: "hover",
        label: "cat_name",
        value: "cat_id",
        children: "children"
      },
      cateInfo: {
        pagenum: 1,
        pagesize: 10
      },
      total: 0,
      cateLevelTag: {
        0: { type: "", content: "一级" },
        1: { type: "success", content: "二级" },
        2: { type: "warning", content: "三级" }
      },
      cateColumnsCfg: [
        {
          label: "分类名称",
          prop: "cat_name"
        },
        {
          label: "是否有效",
          type: "template",
          template: "cat_deleted"
        },
        {
          label: "排序",
          type: "template",
          template: "cat_level"
        },
        {
          label: "操作",
          type: "template",
          template: "setting" //  插槽名称
        }
      ],
      addCateDialog: false,
      selectedKeys: [],
      //  添加分类数据对象
      addCateForm: {
        cat_pid: 0, //  父级ID
        cat_name: "", //  分类名
        cat_level: 0 // 分类等级，默认一级
      },
      addCateRules: {
        cat_name: [
          {
            required: true,
            message: "请输入分类名称",
            trigger: "blur"
          }
        ]
      }
    };
  },
  created() {
    this.getCateList();
  },
  methods: {
    async getCateList() {
      const { data: res } = await this.$http.get("categories", {
        params: this.cateInfo
      });
      if (res.meta.status !== 200)
        return this.$message.error("获取商品分类数据列表失败,请重试!");
      this.cateList = res.data.result;
      this.total = res.data.total;
    },
    handleSizeChange(newSize) {
      this.cateInfo.pagesize = newSize;
      this.getCateList();
    },
    handleCurrentChange(current) {
      this.cateInfo.pagenum = current;
      this.getCateList();
    },
    //  获取父级分类的数据列表
    async getParentCateList() {
      const { data: res } = await this.$http.get("categories", {
        params: { type: 2 }
      });
      if (res.meta.status !== 200)
        return this.$message.error("获取父级分类失败，请重试!");
      this.parentCateList = res.data;
    },
    async getAddCateDialog() {
      this.getParentCateList();
      this.addCateDialog = true;
    },
    async addCate() {
      this.$refs.addCateFormRef.validate(async (valid) => {
        if (!valid) return;
        const { data: res } = await this.$http.post(
          "categories",
          this.addCateForm
        );
        if (res.meta.status !== 201) {
          return this.$message.error("添加失败,请重试!");
        }
        this.getCateList();
        this.$message.success("添加分类成功!");
        this.addCateDialog = false;
      });
    },
    async parentCateChange() {
      if (!this.selectedKeys.length) {
        this.addCateForm.cat_pid = 0;
        this.addCateForm.cat_level = 0;
        return;
      }
      /* const { data: res } = await this.$http.get(
        `categories/${this.selectedKeys[this.selectedKeys.length - 1]}`
      );
      if (res.meta.status !== 200) {
        console.log(this.selectedKeys[this.selectedKeys.length - 1]);
        return this.$message.error("此分类异常，请重试!");
      } */
      this.addCateForm.cat_pid =
        this.selectedKeys[this.selectedKeys.length - 1];
      this.addCateForm.cat_level = this.selectedKeys.length;
    },
    addCateDialogClose() {
      this.$refs.addCateFormRef.resetFields();
    }
  }
};
</script>

<style lang="less" scoped>
.treeTable {
  margin-top: 15px;
}

.el-icon-success {
  color: #12cb12;
}
.el-icon-error {
  color: red;
}

.addCateDialog {
  min-width: 800px;
}
</style>
